import SwiftUI

struct IPInfoView: View {
    @StateObject private var viewModel = IPInfoViewModel()
    @Environment(\.presentationMode) var presentationMode
    var body: some View {
        NavigationView {
            ZStack {
                MainView()
                VStack {
                    Back(title: "Ip Info", onBackTap: {
                        self.presentationMode.wrappedValue.dismiss()
                    }, onImageTap: {})
                    VStack {
                        if let ipInfo = viewModel.ipInfo {
                            VStack(alignment: .leading, spacing: 10) {
                                Text("IP: \(ipInfo.ip)")
                                Text("Hostname: \(ipInfo.hostname)")
                                Text("City: \(ipInfo.city)")
                                Text("Region: \(ipInfo.region)")
                                Text("Country: \(ipInfo.country)")
                                Text("Location: \(ipInfo.loc)")
                                Text("Organization: \(ipInfo.org)")
                                Text("Postal: \(ipInfo.postal)")
                                Text("Timezone: \(ipInfo.timezone)")
                            }
                           
                            .foregroundColor(.white)
                        } else if let errorMessage = viewModel.errorMessage {
                            Text("Error: \(errorMessage)")
                                .foregroundColor(.white)
                                .padding()
                                .background(Color.red.opacity(0.8))
                                .cornerRadius(10)
                        } else {
                            ProgressView("Loading...")
                                .progressViewStyle(CircularProgressViewStyle())
                                .padding()
                                .foregroundColor(.white)
                        }
                    }
                    .padding()
                    .background(Color.gray.opacity(0.2))
                    .cornerRadius(10)
                    .padding()
                    
                    Spacer()
                }
            } .navigationBarHidden(true)
                .navigationBarBackButtonHidden(true)
                .navigationViewStyle(StackNavigationViewStyle())
        } .navigationBarHidden(true)
            .navigationBarBackButtonHidden(true)
            .navigationViewStyle(StackNavigationViewStyle())
    }
}

#Preview {
    IPInfoView()
}
